package com.example.administrator.capital.base;
import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.view.View;


import com.example.administrator.capital.eventbus.EventBase;
import com.example.administrator.capital.home.HomeFragment;
import com.example.administrator.capital.utils.LogUtils;
import com.example.administrator.capital.utils.ToastUtil;

import eventbusactivityscope.EventBusActivityScope;
import fragmentation.SupportFragment;
import rxpermissions2.RxPermissions;

/**
 * 懒加载
 * Created by YoKeyword on 16/6/5.
 * tab的fragment
 */
public   class BaseMainFragment extends SupportFragment implements View.OnClickListener{
    // 再点一次退出程序时间设置
    private static final long WAIT_TIME = 2000L;
    private long TOUCH_TIME = 0;
    protected BaseActivity mBaseActivity;
    protected RxPermissions rxPermissions;
    /*高度用于判断tsb界面是否在顶部*/
    protected int mScrollTotal;
    /*tab界面是否位于顶部*/
    protected boolean mInAtTop = true;
    public BaseMainFragment() {
    }
    @Override
    public void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
         rxPermissions = new RxPermissions(getActivity());
          /*eventbus注册，这里不在base处理，具体在需要的界面处理*/
    }

    @Override
    public void onViewCreated(View view, @Nullable Bundle savedInstanceState) {
        super.onViewCreated(view, savedInstanceState);
        mBaseActivity = (BaseActivity) getActivity();
        initView(view);
        setListener();
        setData();
    }
    protected void initView(View view) {
    }
    protected void setListener() {
    }
    protected void setData() {
    }
    /**
     * 处理回退事件
     *
     * @return
     */
    @Override
    public boolean onBackPressedSupport() {
        if (getChildFragmentManager().getBackStackEntryCount() > 1) {
            LogUtils.d("ddddd");
            popChild();
        } else {
            if (this instanceof HomeFragment) {   // 如果是 第一个Fragment 则退出app
        if (System.currentTimeMillis() - TOUCH_TIME < WAIT_TIME) {
//            _mActivity.moveTaskToBack(true);
            _mActivity.finish();
        } else {
            TOUCH_TIME = System.currentTimeMillis();
            ToastUtil.showShort("再点击一次推出");
        }
            } else {                                    // 如果不是,则回到第一个Fragment
                EventBusActivityScope.getDefault(_mActivity).post(new EventBase("回到首页"));
            }
        }
//        if (System.currentTimeMillis() - TOUCH_TIME < WAIT_TIME) {
////            _mActivity.moveTaskToBack(true);
//            _mActivity.finish();
//        } else {
//            TOUCH_TIME = System.currentTimeMillis();
//            ToastUtil.showShort("再点击一次推出");
//        }
        return true;
    }
    protected void showActivity(Class<? extends BaseActivity> activity) {
        Intent intent = new Intent(mBaseActivity, activity);
       startActivity(intent);
    }
    @Override
    public void onDestroyView() {
        super.onDestroyView();
         /*eventbus注销*/
        EventBusActivityScope.getDefault(_mActivity).unregister(this);
    }

    @Override
    public void onClick(View view) {

    }
}
